package com.example.wordbook;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DetialFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DetialFragment extends Fragment {
    private Dbhelper dbhelper;
    private Dbmanager dbmanager;
    private View viewroot;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public DetialFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment DetialFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static DetialFragment newInstance(String param1, String param2) {
        DetialFragment fragment = new DetialFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        viewroot=inflater.inflate(R.layout.fragment_detial, container, false);
        dbhelper=new Dbhelper(viewroot.getContext());
        dbmanager=new Dbmanager(dbhelper);
        Button button=viewroot.findViewById(R.id.check);
        EditText word=viewroot.findViewById(R.id.checkword);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<String> arrayList =dbmanager.selectData(word.getText().toString().trim());
                if(arrayList!=null) {
                    TextView textView1 = viewroot.findViewById(R.id.eglish);
                    TextView textView2 = viewroot.findViewById(R.id.chin);
                    TextView TextView3 = viewroot.findViewById(R.id.sam);
                    textView1.setText(arrayList.get(0));
                    textView2.setText(arrayList.get(1));
                    TextView3.setText(arrayList.get(2));
                }
                else {
                    TextView textView=viewroot.findViewById(R.id.tip);
                    textView.setText("查询有误");
                }
            }
        });
        word.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                TextView textView1 = viewroot.findViewById(R.id.egword1);
                TextView textView2 = viewroot.findViewById(R.id.egword2);
                TextView textView3 = viewroot.findViewById(R.id.egword3);
                TextView textView4 = viewroot.findViewById(R.id.egword4);
                TextView textView5 = viewroot.findViewById(R.id.egword5);
                textView1.setText("");
                textView2.setText("");
                textView3.setText("");
                textView4.setText("");
                textView5.setText("");
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String egword=word.getText().toString().trim();
                ArrayList<String> firstword;
                if(!egword.isEmpty()) {
                    firstword = dbmanager.quey(egword);
                    TextView textView1 = viewroot.findViewById(R.id.egword1);
                    TextView textView2 = viewroot.findViewById(R.id.egword2);
                    TextView textView3 = viewroot.findViewById(R.id.egword3);
                    TextView textView4 = viewroot.findViewById(R.id.egword4);
                    TextView textView5 = viewroot.findViewById(R.id.egword5);
                    int i = firstword.size();
                    if (i >= 5) {
                        textView1.setText(firstword.get(0));
                        textView2.setText(firstword.get(1));
                        textView3.setText(firstword.get(2));
                        textView4.setText(firstword.get(3));
                        textView5.setText(firstword.get(4));
                    } else if (i >= 4) {
                        textView1.setText(firstword.get(0));
                        textView2.setText(firstword.get(1));
                        textView3.setText(firstword.get(2));
                        textView4.setText(firstword.get(3));
                    } else if (i >= 3) {
                        textView1.setText(firstword.get(0));
                        textView2.setText(firstword.get(1));
                        textView3.setText(firstword.get(2));
                    } else if (i >= 2) {
                        textView1.setText(firstword.get(0));
                        textView2.setText(firstword.get(1));
                    } else if (i >= 1) {
                        textView1.setText(firstword.get(0));
                    }
                    else {
                        textView1.setText("");
                        textView2.setText("");
                        textView3.setText("");
                        textView4.setText("");
                        textView5.setText("");

                    }
                }
            }
        });
        return viewroot;
    }
}